// 在DOM加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
  // 初始化AOS动画库
  AOS.init({
    duration: 800,
    once: true,
    mirror: false
  });
  
  // 导航栏滚动效果
  const navbar = document.querySelector('.navbar');
  
  window.addEventListener('scroll', () => {
    if (window.scrollY > 50) {
      navbar.classList.add('navbar-scrolled');
    } else {
      navbar.classList.remove('navbar-scrolled');
    }
  });
  
  // 导航链接平滑滚动
  document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function(e) {
      e.preventDefault();
      
      const targetId = this.getAttribute('href');
      if (targetId === '#') return;
      
      const targetElement = document.querySelector(targetId);
      if (targetElement) {
        window.scrollTo({
          top: targetElement.offsetTop - 70,
          behavior: 'smooth'
        });
        
        // 移动端菜单自动关闭
        const navbarToggler = document.querySelector('.navbar-toggler');
        const navbarCollapse = document.querySelector('.navbar-collapse');
        if (navbarCollapse.classList.contains('show')) {
          navbarToggler.click();
        }
      }
    });
  });
  
  // 高亮当前活动导航项
  const sections = document.querySelectorAll('section');
  
  window.addEventListener('scroll', () => {
    let current = '';
    
    sections.forEach(section => {
      const sectionTop = section.offsetTop;
      const sectionHeight = section.clientHeight;
      if (window.scrollY >= (sectionTop - 100)) {
        current = section.getAttribute('id');
      }
    });
    
    document.querySelectorAll('.nav-link').forEach(link => {
      link.classList.remove('active');
      if (link.getAttribute('href') === `#${current}`) {
        link.classList.add('active');
      }
    });
  });
  
  // 表单提交处理
  const contactForm = document.getElementById('contactForm');
  if (contactForm) {
    contactForm.addEventListener('submit', function(e) {
      e.preventDefault();
      
      // 获取表单数据
      const formData = new FormData(this);
      const formValues = Object.fromEntries(formData.entries());
      
      // 这里可以添加表单数据验证逻辑
      
      // 模拟表单提交
      // 实际应用中，这里应该将数据发送到后端或表单服务
      console.log('表单数据：', formValues);
      
      // 显示提交成功消息
      const submitButton = contactForm.querySelector('button[type="submit"]');
      const originalText = submitButton.innerHTML;
      
      submitButton.disabled = true;
      submitButton.innerHTML = '提交中...';
      
      // 模拟网络请求延迟
      setTimeout(() => {
        const alertBox = document.createElement('div');
        alertBox.className = 'alert alert-success mt-3';
        alertBox.textContent = '您的信息已成功提交，我们会尽快与您联系！';
        contactForm.appendChild(alertBox);
        
        // 重置表单
        contactForm.reset();
        submitButton.disabled = false;
        submitButton.innerHTML = originalText;
        
        // 5秒后隐藏成功消息
        setTimeout(() => {
          alertBox.remove();
        }, 5000);
      }, 1500);
    });
  }
}); 